﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


using System.Collections;
using System.Reflection;

//using System;
//using System.Data;
//using System.Configuration;
//using System.Web;
//using System.Web.Security;
//using System.Web.UI;
//using System.Web.UI.WebControls;
//using System.Web.UI.WebControls.WebParts;
//using System.Web.UI.HtmlControls;
//using System.Collections;
//using System.Reflection;

namespace TerminologiaSinacor.Utility
{
	public class BindingUtil
	{
		public static void SetDataSource(Object list, ListControl listControl, String dataValueField, String dataTextField)
		{
			SetDataSource(list, listControl, dataValueField, dataTextField, String.Empty, "[Selecione]");
		}

		public static void SetDataSource(Object list, ListControl listControl, String dataValueField, String dataTextField, String firstDataValue, String firstDataText)
		{
			listControl.Items.Clear();
			listControl.DataSource = list;
			listControl.DataValueField = dataValueField;
			listControl.DataTextField = dataTextField;
			listControl.DataBind();

			if (listControl is DropDownList)
				listControl.Items.Insert(0, new ListItem(firstDataText, firstDataValue));
		}

		public static void SetDataSourceManual(ICollection list, ListControl listControl, String dataValueField, String dataTextField)
		{
			SetDataSourceManual(list, listControl, dataValueField, dataTextField, String.Empty, "[Selecione]");
		}

		public static void SetDataSourceManual(ICollection list, ListControl listControl, String dataValueField, String dataTextField, String firstDataValue, String firstDataText)
		{
			listControl.Items.Clear();

			foreach (Object item in list)
				listControl.Items.Add(new ListItem(DataBinder.Eval(item, dataTextField).ToString(), DataBinder.Eval(item, dataValueField).ToString()));

			if (listControl is DropDownList)
				listControl.Items.Insert(0, new ListItem(firstDataText, firstDataValue));
		}

	}
}

